﻿namespace Shanda.DevFx.Security.Web
{
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Utils;
    using System;
    using System.Security.Permissions;
    using System.Web.UI;

    public class BasePage : Page
    {
        private Uri returnUri;
        private string returnUrl;

        protected virtual void InitData()
        {
        }

        protected virtual void InitReturnUrl()
        {
            if (this.returnUrl == null)
            {
                Uri toUri = null;
                string relativeUri = null;
                if (base.get_IsPostBack())
                {
                    relativeUri = (string) this.get_ViewState().get_Item("shanda.devfx.security.web.basepage.returnurl");
                    if (relativeUri != null)
                    {
                        this.returnUri = new Uri(base.get_Request().get_Url(), relativeUri);
                        this.returnUrl = string.Format("window.location.href='{0}'", relativeUri);
                        return;
                    }
                    relativeUri = base.get_Request().get_QueryString()["ReturnUrl"];
                    if (Checker.CheckEmptyString("ReturnUrl", relativeUri, false))
                    {
                        this.returnUrl = "javascript:history.back()";
                        return;
                    }
                }
                relativeUri = base.get_Request().get_QueryString()["ReturnUrl"];
                if (Checker.CheckEmptyString("ReturnUrl", relativeUri, false))
                {
                    toUri = base.get_Request().get_UrlReferrer();
                }
                else
                {
                    toUri = new Uri(base.get_Request().get_Url(), relativeUri);
                }
                if (toUri != null)
                {
                    relativeUri = base.get_Request().get_Url().MakeRelative(toUri) + toUri.Query;
                    if (string.Compare(relativeUri, toUri.ToString(), true) == 0)
                    {
                        toUri = null;
                    }
                }
                if (toUri != null)
                {
                    this.returnUri = toUri;
                    this.returnUrl = string.Format("window.location.href='{0}'", relativeUri);
                    this.get_ViewState().Add("shanda.devfx.security.web.basepage.returnurl", relativeUri);
                }
                else
                {
                    this.returnUrl = "javascript:history.back()";
                }
            }
        }

        [Permission("19001"), PermissionSet(SecurityAction.Demand), PrincipalPermission(SecurityAction.Demand, Role="Administrators")]
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.InitReturnUrl();
            this.InitData();
        }

        protected virtual Uri ReturnUri
        {
            get
            {
                if (this.returnUrl == null)
                {
                    this.InitReturnUrl();
                }
                return this.returnUri;
            }
        }

        protected virtual string ReturnUrl
        {
            get
            {
                if (this.returnUrl == null)
                {
                    this.InitReturnUrl();
                }
                return this.returnUrl;
            }
        }
    }
}

